$(function(){
    /**初始化标签得内容**/
    $.post(ctx+"saleConfig/selectSaletag",{},function (r) {
        if (r.code == 0){
            var data = r.msg;
            $.each(data,function (index,val) {
                var pid = val.parent.tagUuid;
                var div =  $(' <div class="text-left border-bottom-1" pid="'+pid+'" typeid="1"></div>');
                var fistdiv  ='<div class="border-bottom-1 p-t-3"><div class="checkbox m-0 "><span class="m-r-15 m-l-5">'+val.parent.tagGroupName+'</span>' +
                    '<select class="width-200 form-control  inline-block m-b-3" onchange="selectLabelTypeadd(this)" ><option value="1">保持本组原有得标签</option>' +
                    '<option value="2">不添加本组标签,删除原有标签</option><option value="3">使用新的标签替换本组标签</option></select>' +
                    '<label><input type="hidden" value='+val.parent.tagSelect+' /></label></div></div>';
                div.append(fistdiv);
                var cc = $("<div class='m-t-5 p-3 text-left'><input type='hidden' value="+val.parent.tagSelect+" /></div>");
                $.each(val.children,function (i,v) {
                    cc.append('<button type="button" disabled onclick="selectTagIscheckAdd(this)" data-id="'+v.tagUuids+'" ' +
                        'class="btn btn-xs btn-link width-100 m-l-5 p-2 m-b-5 f-s-12 border-solid-g">'+v.tagName+'</button>');
                });
                div.append(cc);
                $("#selectTagAdd").append(div);
            });
        }else{
            $JuXue.n_danger(r.msg);
        }
    });
    /**初始化文件上传**/
    init_personEmp()
});
/**初始化批量导入**/
function init_personEmp(){
    // 同步步骤执行
    $('#smartwizardExport').smartWizard({
        selected: 0,
        theme: 'default',
        transitionEffect:'fade',
        useURLhash:false,
        showStepURLhash:false,
        lang: { // Language variables for button
            next: '下一步', previous: '上一步'
        },
    });
    $("#smartwizardExport .sw-btn-group").addClass("pull-right");
    // Smart Wizard events
    $("#smartwizardExport").on("leaveStep", function(e, anchorObject, stepNumber, stepDirection) {
        if(stepDirection === "forward"){
            if (stepNumber === 0){
                //第一步 校对表头信息
                var path = $("#exportExcelPathHide").val();
                if(path === ""){$JuXue.n_warning("获取文件失败,请返回上一步重新导入");return ;}
                var load = new Loading();
                load.init();
                load.start();
                //第二步 校对成功 制定导入
                $.post(ctx+"PerSonCustomer/PersonValiableExport",{expUrl:path},function (r) {
                    if(r.code === 0){
                        setTimeout(function() {
                            load.stop();
                            $("#vaStep2Export").text(r.msg);$("#vaStep2StatusExport").val("0");
                        }, 3000)
                    }else {
                        setTimeout(function() {
                            load.stop();
                            $("#vaStep2Export").text(r.msg);$("#vaStep2StatusExport").val("1");
                        }, 3000)
                    }
                });
            }else if (stepNumber == 1) {
                //填写基础公用的表单
                var vaValue = $("#vaStep2StatusExport").val();
                if(vaValue === "1"){$JuXue.n_warning("数据验证失败,不能导入信息");return ;}
                var path = $("#exportExcelPathHide").val();
                if(path === ""){$JuXue.n_warning("获取文件失败,请返回上一步重新导入");return ;}
                var load = new Loading();
                load.init();
                load.start();
                //最后一步 保存数据库
                $.post(ctx+"PerSonCustomer/batchPersonInsert",{expUrl:path},function (r) {
                    load.stop();
                    if(r.code === 0){$("#vastep3Export").text("导入成功："+r.msg+" 数目");$JuXue.refreshTable('myEmpTable');}else{$("#vastep3Export").text("导入失败");}
                });
            }
            return true;
        }
        return true;
    });
    //文件上传验证
    $("#fileExport").dropzone({
        url: ctx+"bigEpResoure/uploadFile", //必须填写
        method:"post",  //也可用put
        paramName:"file", //默认为file
        maxFiles:1,//一次性上传的文件数量上限
        maxFilesize: 10, //MB
        acceptedFiles: ".xls,.xlsx", //上传的类型
        parallelUploads: 1,
        dictMaxFilesExceeded: "您最多只能上传1个文件！",
        dictResponseError: '文件上传失败!请联系管理员',
        dictInvalidFileType: "你不能上传该类型文件,文件类型只能是*.xls,*.xlsx",
        dictFallbackMessage:"浏览器不受支持",
        dictFileTooBig:"文件过大上传文件最大支持.",
        dictDefaultMessage:"点击上传文件",
        dictRemoveFile:"删除上传文件",
        init:function(){
            this.on("success",function (o,r) {
                if (r.code===0){$JuXue.n_success("上传文件成功");$("#exportExcelPathHide").val(r.data[0]);return ;}
                $JuXue.n_danger("文件上传失败,请刷新后重试");
            });
            this.on("maxfilesexceeded",function () {
                $JuXue.n_danger("只能上传一个文件");return ;
            });
        }
    });
}

/**下载模板**/
function downPersonModal(){
    var url = ctx + "PerSonCustomer/downPersonModul";
    $.post(url,{},function (r) {
        if (r.code === 0) {window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        } else {$JuXue.n_danger(r.msg);}
    });
}
/**保存信息**/
function saveBigEmpInfo(btn) {
    $(btn).attr("disabled","disabled");
    var myCusName = $("#myCusName").val();
    if (myCusName == ""){$JuXue.n_danger("客户姓名不能为空");$(btn).removeAttrs("disabled");return }
    var mobile = $("#myCusMobile").val();
    //增加标签
    var form_data = $("#add_myEmp_form").serialize();
    $.post(ctx+"PerSonCustomer/addMyCustomer",form_data,function (r) {
        if (r.code === 0) {
            var data = r.data;
            if(r.msg=="客户重复"){
                $("#myCustomer-modal").empty().modal("hide");
                var name = data.nickname;
                var receiveid = data.distributor;
                var uuid = data.id;
                var reid = data.receiver;
                $.post(ctx+"myCustomer/loadOtherModal",{},function (r) {
                    $("#other-modal").empty().append(r).modal('show');
                    $("#other-name").text(name);
                    $("#other-receiver").text(receiveid);
                    $("#copyMobileId").text(mobile);
                    $("#shenCopyId").attr("uuid",uuid);
                    $("#shenCopyId").attr("reid",reid);
                    $("#shenCopyId").attr("name",name);
                });
            }else if(r.msg=="myself"){
                $("#myCustomer-modal").empty().modal("hide");
                var uuid = data.id;
                $("#customer_uuid").val(uuid);
                $("#customer_ids").val(uuid);
                $.post(ctx+"myCustomer/loadMyselfModal",{},function (r) {
                    $("#myself-modal").empty().append(r).modal('show');
                });
            }else{
                $("#myCustomer-modal").empty().modal("hide");
                $JuXue.n_success(r.msg);
                $JuXue.refreshTable('myEmpTable');
            }
        } else {
            $(btn).removeAttrs("disabled");
            $JuXue.n_danger(r.msg);
        }
    });
}

/**申请共享*/
function shenqingCopy(btn) {
    var uuid = $(btn).attr("uuid");
    var reid = $(btn).attr("reid");
    var name = $(btn).attr("name");
    if(uuid==""||uuid==null||reid==null||reid==""){
        $JuXue.n_danger("获取数据失败，请联系网站管理员！");return;}
    var content = $("#pleasecont").val();
    $.post(ctx+"loadMyselfModal/shenqingShare",{"uuid":uuid,"reid":reid,"content":content,"name":name},function (r) {
        if(r.code===0){
            $("#other-modal").empty().modal("hide");
            $JuXue.n_success(r.msg);
        }
    });
}
/**切换选择类型**/
function selectLabelTypeadd(btn) {
    var option = $(btn).val();
    if (option == "1" ||option =="2"){
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).attr("disabled","disabled");
            $(this).removeClass("btn-primary");
        });
    }else {
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {$(this).removeAttrs("disabled");});
    }
    $(btn).parent().parent().parent().attr("typeid",option);
}
/**选择标签**/
function selectTagIscheckAdd(btn) {
    var $parentcheck  = $(btn).parent();
    var isMultiInput = $parentcheck.children("input:first-child").val();
    if (isMultiInput == "1"){
        //多选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {$(btn).removeClass("btn-primary");
        }else{$(btn).addClass("btn-primary");}
    }else{
        //单选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {$parentcheck.find("button").each(function () {$(this).removeClass("btn-primary");});
        }else{
            $parentcheck.find("button").each(function () {$(this).removeClass("btn-primary");});
            $(btn).addClass("btn-primary");
        }
    }
}

/**确定标签**/
function labelSelectSureAdd() {
    var div = $("#selectTagAdd").children("div");
    //包含
    var selectids = new Array();
    var selectnames = new Array();
    $.each(div,function (index,val) {
        var btn = $(this).find("button");
        $.each(btn,function () {
            var isSelect = $(this).hasClass("btn-primary");
            if (isSelect){
                var selectid = $(this).attr("data-id");
                var selectname = $(this).text();
                selectids.push(selectid);selectnames.push(selectname);
            }
        });
    });
    var length = selectids.length;
    if(length > 10){$JuXue.n_danger("标签选择不能大于10个");return ;}
    $("#drownSelectAdd").empty();
    $.each(selectnames,function (i,v) {
        $("#drownSelectAdd").append('<a type="button" class="btn btn-link m-5 p-3 f-s-10 btn-primary ">'+v+'</a>');
    });
    $("#myCusLabel").val(selectids.join(","));
    $("#labelselectadd").removeClass("open");
}
